package interactions
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	import enemies.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class Trapdoor extends Interaction
	{
		private static const OPENED:uint = 0;
		private static const CLOSED:uint = 1;
		private static const CLOSING:uint = 2;
		
		public function Trapdoor(i:uint = 0, j:uint = 0)
		{
			super(i, j);
			
			_timer.duration = 5000;
			
			_cooldownBar.setRange(0, _timer.duration / 1000);
			
			loadGraphic(Assets.ImgInteractionTrapdoor, true);
			
			addAnimation("opened", [3, 4, 5], 1, true);
			addAnimation("closing", [3, 2, 1, 0], 6, false);
			addAnimation("closed", [0], 0, false);
			play("closed");
			
			state = CLOSED;
		}
		
		override public function update():void
		{
			super.update();
			
			if (state == OPENED)
			{
				_cooldownBar.visible = true;
				if (_timer.hasExpired)
				{
					close();
				}
			}
			
			if (state == CLOSING)
			{
				if (finished)
				{
					state = CLOSED;
					_showSpacebar = true;
				}
			}
		}
		
		override public function overlapWithEnemy(e:Enemy):void
		{
			if (state == OPENED)
			{
				if (!(e is Brute)) e.visible = false;
				e.shouldDropTreasure = false;
				e.translateToTileByPoint(getMidpoint());
				e.stopMoving();
				e.kill();
			}
		}
		
		override protected function interactWithPlayer(p:Player):void
		{
			open();
		}
		
		private function open():void
		{
			if (state == CLOSED)
			{
				_showSpacebar = false;
				state = OPENED;
				play("opened");
				FlxG.play(Assets.SndTrapdoorOpen, Registry.soundVolume);
				_timer.start();
			}
		}
		
		private function close():void
		{
			if (state == OPENED)
			{
				state = CLOSING;
				play("closing");
				FlxG.play(Assets.SndTrapdoorClose, Registry.soundVolume);
			}
		}
	}
}